package com.gollum.system.service.impl;

import com.gollum.common.context.PageContextHolder;
import com.gollum.common.domain.response.TableSupport;
import com.gollum.common.exception.ServiceException;
import com.gollum.common.utils.BeanUtils;
import com.gollum.common.utils.StringUtils;
import com.gollum.jpa.common.Specifications;
import com.gollum.jpa.domain.Page;
import com.gollum.jpa.service.JpaServerImpl;
import com.gollum.system.domain.dao.SysRoleDao;
import com.gollum.system.domain.model.SysDictData;
import com.gollum.system.domain.model.SysLogininfor;
import com.gollum.system.domain.model.SysRole;
import com.gollum.system.domain.model.SysUser;
import com.gollum.system.domain.request.SysDictDataRequest;
import com.gollum.system.domain.request.SysRoleRequest;
import com.gollum.system.domain.request.SysUserRequest;
import com.gollum.system.domain.response.SysDictDataResponse;
import com.gollum.system.domain.response.SysRoleResponse;
import com.gollum.system.service.SysRoleService;
import com.gollum.system.service.SysUserService;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * 角色service实现
 *
 * @author Herther
 * @createTime 2022年09月06日 22:05:00
 * @since 1.0.0
 */
@Service
public class SysRoleServiceImpl extends JpaServerImpl<SysRole, Long> implements SysRoleService {

    @Autowired
    protected SysRoleDao sysRoleDao;

    @Autowired
    protected SysUserService sysUserService;

    /**
     * 构建查询参数 init
     * @auther: Herther
     * @since 1.0.0
     * @date: 2022/9/18 23:22
     */
    private Specification<SysRole> getParams(SysRoleRequest sysRoleRequest){
        Specification<SysRole> spec = Specifications.<SysRole>and()
                //角色名称
                .like(StringUtils.isNotEmpty(sysRoleRequest.getRoleName()),
                        SysRole::getRoleName,sysRoleRequest.getRoleName())
                // 角色字符串
                .like(StringUtils.isNotEmpty(sysRoleRequest.getRoleKey()),
                        SysRole::getRoleKey, sysRoleRequest.getRoleKey())
                //状态（0正常 1停用）
                .eq(StringUtils.isNotEmpty(sysRoleRequest.getStatus()),
                        SysRole::getStatus, sysRoleRequest.getStatus())
                //创建时间范围
                .between(getBetweenCondition(sysRoleRequest.getBeginTime()
                                ,sysRoleRequest.getEndTime())
                        , SysRole::getCreateTime
                        , sysRoleRequest.getBeginTime(),sysRoleRequest.getEndTime())
                .build();
        return spec;
    }

    @Override
    public TableSupport<SysRoleResponse> listPage(SysRoleRequest params) {
        Page<SysRole> page = PageContextHolder.get().toPage();
        page = this.findAllWithPage(page,getParams(params));
        return TableSupport.toTableResponse(page, BeanUtils.copyToList(page.getRecords(), SysRoleResponse.class));
    }

    @Override
    public Set<String> getRoleSetByUser(Long userId) {
        SysUser sysUser = sysUserService.getById(userId);
        if (Objects.isNull(sysUser)){
            throw new ServiceException("用户不存在，既没有角色");
        }
        HashSet<String> rolesSet = new HashSet<>();
        //如果是超级管理员就不需要去做其他查询直接赋值admin
        if (sysUser.isAdmin()){
            rolesSet.add("admin");
            return rolesSet;
        }
        List<SysRole> roles = sysUser.getRoles();
        //拿到role key
        roles.forEach(role->{
            rolesSet.add(role.getRoleKey());
        });
        return rolesSet;
    }
}
